//
//  UIView+MKSAdd.swift
//  huaya-jisu
//
//  Created by MK on 2021/7/29.
//  Copyright © 2021 taolang. All rights reserved.
//

import Foundation

extension UIView {
    @objc open var x: CGFloat {
        get { return self.frame.origin.x }
        set {
            var frame = self.frame;
            frame.origin.x = newValue;
            self.frame = frame;
        }
    }
    
    @objc open var y: CGFloat {
        get { return self.frame.origin.y }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    @objc open var width: CGFloat {
        get { return self.frame.size.width }
        set {
            var frame = self.frame;
            frame.size.width = newValue;
            self.frame = frame;
        }
    }
    
    @objc open var height: CGFloat {
        get { return self.frame.size.height }
        set {
            var frame = self.frame;
            frame.size.height = newValue;
            self.frame = frame;
        }
    }
    
    @objc open var origin: CGPoint {
        get { return self.frame.origin }
        set {
            var frame = self.frame;
            frame.origin = newValue;
            self.frame = frame;
        }
    }
    
    @objc open var size: CGSize {
        get { return self.frame.size }
        set {
            var frame = self.frame;
            frame.size = newValue;
            self.frame = frame;
        }
    }
    
    @objc open var centerX: CGFloat {
        get { return self.center.x }
        set {
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    @objc open var centerY: CGFloat {
        get { return self.center.y }
        set {
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
   @objc open var top : CGFloat {
        get { return self.frame.origin.y }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    @objc open var bottom : CGFloat {
        get { return frame.origin.y + frame.size.height }
        set {
            var frame = self.frame
            frame.origin.y = newValue - self.frame.size.height
            self.frame = frame
        }
    }
    
    @objc open var left : CGFloat {
        get { return self.frame.origin.x }
        set {
            var frame = self.frame
            frame.origin.x  = newValue
            self.frame = frame
        }
    }
    
    @objc open var right : CGFloat {
        get { return self.frame.origin.x + self.frame.size.width }
        set {
            var frame = self.frame
            frame.origin.x = newValue - self.frame.size.width
            self.frame = frame
        }
    }
}


extension UIView {
    /**
     * 设置圆角
     */
    @objc open func setCorner(radii : CGFloat) {
        self.setCorner(radii: CGSize(width: radii, height: 0), corners: .allCorners);
    }
    
    @objc open func setCircle() {
        self.setCorner(radii:CGSize(width: self.width/2.0, height: 0), corners: .allCorners);
    }
    
    @objc open func setCorner(radii : CGSize, corners : UIRectCorner = .allCorners){
        let maskPath = UIBezierPath (roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: radii);
        let maskLayer = CAShapeLayer();
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.cgPath;
        self.layer.mask = maskLayer;
    }
    
    /**
     * 截图
     */
    public func screenshot(size: CGSize? = nil, offset: CGPoint? = nil, quality: CGFloat = 1) -> UIImage? {
        assert(0...1 ~= quality);
        
        let size = size ?? self.bounds.size;
        let offset = offset ?? .zero;
        
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale * quality);
        drawHierarchy(in: CGRect(origin: offset, size: frame.size), afterScreenUpdates: false);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
        
    }
    
    @objc open func removeAllSubviews() {
        while self.subviews.count > 0 {
            self.subviews.last?.removeFromSuperview()
        }
    }
}
